package cc.mrbird.unit.service.impl;

import cc.mrbird.common.domain.QueryRequest;
import cc.mrbird.common.domain.Tree;
import cc.mrbird.common.service.impl.BaseService;
import cc.mrbird.grid.dao.GridInfoMapper;
import cc.mrbird.grid.domain.GridInfo;
import cc.mrbird.grid.service.GridInfoService;
import cc.mrbird.system.domain.User;
import cc.mrbird.unit.dao.UnitTypeMapper;
import cc.mrbird.unit.domain.UnitType;
import cc.mrbird.unit.service.UnitTypeService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service("unitTypeService")
public class UnitTypeServiceImpl extends BaseService<UnitType> implements UnitTypeService {

    private Logger log = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private UnitTypeMapper unitTypeMapper;
    @Override
    public List<UnitType> findAllList(UnitType unitType, QueryRequest request) {
        try {
            Example example = new Example(UnitType.class);
            Example.Criteria criteria = example.createCriteria();
            example.setOrderByClause("unit_type_id");
            return this.selectByExample(example);
        } catch (Exception e) {
            log.error("error", e);
            return new ArrayList<>();
        }
    }

    @Override
    public UnitType findByName(String uuit_type_name) {
        Example example = new Example(UnitType.class);
        example.createCriteria().andCondition("lower(unit_type_name)=", uuit_type_name.toLowerCase());
        List<UnitType> list = this.selectByExample(example);
        return list.isEmpty() ? null : list.get(0);
    }

    @Override
    public void add(UnitType unitType) {
        this.save(unitType);
    }

    @Override
    public void update(UnitType unitType) {
        this.updateNotNull(unitType);
    }

    @Override
    public UnitType findById(Long id) {
        return this.selectByKey(id);
    }

    @Override
    @Transactional
    public void delete(String ids) {
        List<String> list = Arrays.asList(ids.split(","));
        this.batchDelete(list, "unit_type_id", UnitType.class);
    }
}
